package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResponse;
import com.heima.entity.*;
import com.heima.mapper.TbRegionMapper;
import com.heima.pojo.vo.TbNodeVo;
import com.heima.pojo.vo.TbRegionVo;
import com.heima.pojo.vo.TbVendingMachineVo;
import com.heima.service.*;
import com.mysql.cj.PreparedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 售货机类型表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-09
 */
@RestController
@RequestMapping("/api/vm-service")
public class TbVmTypeController {
    @Autowired
    private ITbVmTypeService iTbVmTypeService;

    @Autowired
    private ITbVendingMachineService itbVendingMachineService;

    @Autowired
    private ITbRegionService iTbRegionService;

    @Autowired
    private ITbNodeService iTbNodeService;

    @Autowired
    private ITbBusinessService iTbBusinessService;

    @Autowired
    private TbRegionMapper tbRegionMapper;


    @GetMapping("/vmType/search")
    public PageResponse<TbVmType> selectType(@RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                                             @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                             @RequestParam(value = "name",required = false) String name){
        //设置分页
        Page<TbVmType> page = new Page<>(pageIndex,pageSize);

        //构造条件参数
        QueryWrapper<TbVmType> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name",name);
        }
        //执行分页查询
        iTbVmTypeService.page(page,wrapper);

        //封装并返回结果
        PageResponse<TbVmType> response = new PageResponse<>();
        response.setTotalCount((int) page.getTotal());
        response.setPageSize(pageSize);
        response.setPageIndex(pageIndex);
        response.setCurrentPageRecords(page.getRecords());
        return response;
    }
    @PostMapping("/vmType")
    public Boolean addType(@RequestBody TbVmType type){
        TbVmType tbVmType = new TbVmType();
        tbVmType.setName(type.getName());
        tbVmType.setModel(type.getModel());
        tbVmType.setVmRow(type.getVmRow());
        tbVmType.setVmCol(type.getVmCol());
        tbVmType.setChannelMaxCapacity(type.getChannelMaxCapacity());
        tbVmType.setImage(type.getImage());
        boolean flag = iTbVmTypeService.save(tbVmType);
        return flag;
    }
    public Boolean update(@PathVariable("typeId") Integer typeId,
                          @RequestBody TbVmType type){
        TbVmType tbVmType = new TbVmType();
        tbVmType.setTypeId(typeId);
        BeanUtil.copyProperties(type,tbVmType);
        boolean flag = iTbVmTypeService.update(tbVmType, null);
        return flag;
    }
    @DeleteMapping("/vmType/{typeId}")
    public Boolean deleteType(@PathVariable("typeId") Integer typeId){
        boolean flag = iTbVmTypeService.removeById(typeId);
        return flag;
    }
    @GetMapping("/vm/search")
    public PageResponse<TbVendingMachineVo> selectVm(@RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                                                     @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                                     @RequestParam(value = "innerCode",required = false) String innerCode){
        //设置分页
        Page<TbVendingMachine> page = new Page<>(pageIndex,pageSize);

        //构建分页参数
        QueryWrapper<TbVendingMachine> wrapper = new QueryWrapper<>();
        if (innerCode != null) {
           wrapper.like("inner_code",innerCode);
        }
        itbVendingMachineService.page(page,wrapper);


        List<TbVendingMachineVo> machineVoList = page.getRecords().stream().map(r -> {

            TbVendingMachineVo vo = new TbVendingMachineVo();
            //查询类型
            Integer vmType = r.getVmType();
            TbVmType type = iTbVmTypeService.lambdaQuery()
                    .eq(vmType != null, TbVmType::getTypeId, vmType)
                    .one();
            //查询站点
            TbNodeVo tbNodeVo = new TbNodeVo();
            TbNode tbNode = iTbNodeService.getById(r.getNodeId());
            BeanUtil.copyProperties(tbNode,tbNodeVo);
            //统计点位机器数量
            Integer vmCount = itbVendingMachineService.lambdaQuery()
                    .eq(tbNode.getId() != null, TbVendingMachine::getNodeId, tbNode.getId())
                    .count();
            //查询地区
            TbRegionVo tbRegionVo = new TbRegionVo();
            QueryWrapper<TbRegion> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("id", r.getRegionId());
            TbRegion tbRegion = tbRegionMapper.selectOne(wrapper1);
            if(ObjectUtil.isNotEmpty(tbRegion)){
                Long id = tbRegion.getId();
                Integer nodeCount = iTbNodeService.lambdaQuery()
                        .eq(id != null, TbNode::getRegionId, id)
                        .count();
                BeanUtil.copyProperties(tbRegion, tbRegionVo);
                tbRegionVo.setNodeCount(nodeCount);
            }

            //查询类型
            Integer businessId = r.getBusinessId();
            TbBusiness business = iTbBusinessService.lambdaQuery()
                    .eq(businessId != null, TbBusiness::getId, businessId)
                    .one();
            BusinessType businessType = BeanUtil.toBean(business, BusinessType.class);
            tbNodeVo.setVmCount(vmCount);
            tbNodeVo.setBusinessType(businessType);
            tbNodeVo.setRegion(tbRegionVo);

            BeanUtil.copyProperties(r,vo);
            vo.setType(type);
            vo.setNode(tbNodeVo);
            vo.setRegion(tbRegionVo);

            return vo;
        }).collect(Collectors.toList());

        //封装并返回结果
        PageResponse<TbVendingMachineVo> response = new PageResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount((int) page.getTotal());
        response.setCurrentPageRecords(machineVoList);

        return response;
    }
    @PostMapping("/vm")
    public Boolean addVm(@RequestBody TbVendingMachine machine){
        TbVendingMachine machine1 = new TbVendingMachine();
        BeanUtil.copyProperties(machine,machine1);
        machine1.setUpdateTime(LocalDateTime.now());
        machine1.setCreateTime(LocalDateTime.now());
        boolean flag = itbVendingMachineService.save(machine1);
        return flag;
    }
    @Autowired
    private ITbPolicyService iTbPolicyService;
    @GetMapping("/policy/search")
    public PageResponse<TbPolicy> selectPolicy(@RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                                               @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                               @RequestParam(value = "policyName",required = false) String policyName){
        //1.设置分页
        Page<TbPolicy> page = new Page<>(pageIndex,pageSize);
        //构建查询参数
        QueryWrapper<TbPolicy> wrapper = new QueryWrapper<>();
        wrapper.select("policy_id","policy_name","discount","create_time");
        if (policyName != null) {
            wrapper.like("policy_name",policyName);
        }
        //执行分页查询
        iTbPolicyService.page(page,wrapper);

        //封装并返回结果
        PageResponse<TbPolicy> response = new PageResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount((int) page.getTotal());
        response.setCurrentPageRecords(page.getRecords());

        return response;

    }
    @PutMapping("/policy/{policyId}")
    public Boolean updatePolicy(@PathVariable("policyId") Integer policyId,
                          @RequestBody TbPolicy policy){
        TbPolicy tbpolicy = new TbPolicy();
        tbpolicy.setPolicyId(policyId);
        BeanUtil.copyProperties(policy,tbpolicy);
        boolean flag = iTbPolicyService.update(tbpolicy, null);
        return flag;
    }

}
